<?PHP

/**
 *	Set up variables to use within the page.
 *
 *		- mullet: an instance of Mullet class, which is mainly
 *		to return events within a given time and date range. In
 *		this case, we want to print all events from yesterday on.
 */

$mullet = 			new mullet($mul_db, $smf_db);
					$mullet->getEvents(strtotime('yesterday'));





/**
 *	Print out the table header.
 */

echo '
<TABLE class="eventlist">
<TR class="emphasize">
	<TH>Date</TH>
	<TH>Location</TH>
	<TH>Invite Time</TH>
	<TH>Start Time</TH>
	<TH>Attending</TH>
	<TH>Characters</TH>
	<TH>Role</TH>
	<TH>Status</TH>
	<TH>Action</TH>
</TR>';





/**
 *	Loop through each event returned from Mullet class.
 *
 *		- separator row: there's some white space between
 *		each event so it doesn't get too cluttered.
 *
 *		- event_output: array of what we need to output in
 *		each column.
 *
 *		- userCharacterStatus: sorts the user's characters based
 *		on which are subscribed and those that are not. Subscribed
 *		characters can be updated from this page, non-subscribed
 *		are displayed in a drop down menu so users can also
 *		subscribe from this page.
 *			(user instance comes from index page)
 */

foreach ($mullet->events as $event)
{
	// Clear the event output array.
	unset($event_output);
	$event_output =	array();


	// Get event data.
	$event->getSubscribers();
	$event->getTotals();
	$event->userCharacterStatus($user);





	/**
	 *	Setup event row (part I)
	 *
	 *		- this information is printed every row no
	 *		matter what, so we can set it up here instead
	 *		of doing it twice later.
	 */

	// ----------- Date
	$event_output['date'] = date("D n/j/y", $event->start_time);


	// ----------- Location
	$event_output['location'] =
		'<a href="index.php?page=event&amp;eid='.$event->eid.'">'.$event->location.'</a>';


	// ----------- Invite Time
	$event_output['invite_time'] = date("g:ia", $event->invite_time);


	// ----------- Start Time
	$event_output['start_time'] = date("g:ia", $event->start_time);


	// ----------- Statistics
	$event_output['attending'] =
		$event->totals['attending']['total'].' of '.$event->cap.'
		('.$event->totals['available']['total'].')';





	/**
	 *	Setup event row (part II)
	 *
	 *		- if the user has all of their characters subscribed to
	 *		a particular event, we're not going to print out the
	 *		form which allows them to subscribe.
	 *			(no real reason, it would be a blank form)
	 */

	if (count($event->chars_not_subscribed) == 0)
	{
		// ----------- HTML Form
		$event_output['form'] = '';
		$event_output['form2'] = '';


		// ----------- Characters Menu
		$event_output['character'] = ' -- ';


		// ----------- Role
		$event_output['role'] = ' -- ';


		// ----------- Status
		$event_output['status'] = ' -- ';


		// ----------- Action
		$event_output['action'] = '';
	}

	/**
	 *	Setup event row (part III)
	 *
	 *		- the real meat of the row: the user still has characters
	 *		not subscribed to the event, so we need to build a form
	 *		for them to select a character, role, status and an action
	 *		button.
	 */

	else
	{
		// ----------- HTML Form
		$event_output['form'] = '
			<form action="index.php" method="post" id="subscribe_form'.$event->eid.'">
			<input type="hidden" name="process" value="subscribe" />
			<input type="hidden" name="event_id" value="'.$event->eid.'" />
			<input type="hidden" name="is_update" value="0" />
			<input type="hidden" name="comment" value="" />';
		$event_output['form2'] = '</form>';



		// ----------- Characters Menu
		$event_output['character'] = '<select name="user_char" id="user_char">';

		for ($i = 0; $i < count($event->chars_not_subscribed['character']); $i++)
		{
			$event_output['character'] .= '<option value='.strtolower($event->chars_not_subscribed['character'][$i]).'>
			'.ucwords($event->chars_not_subscribed['character'][$i]).'</option>'."\r\n";
		}

		$event_output['character'] .= '</select>';



		// ----------- Role
		$event_output['role'] = '
			<select name="user_role" id="user_role">
			<option value="null">Select one:</option>';

		foreach ($mConfig['roles'] as $abbr => $role)
		{
			($event->chars_not_subscribed['role'][0] == $abbr) ?
				$selected = ' selected = "selected"' :
				$selected = '';
			$event_output['role'] .= '<option value="'.$abbr.'"'.$selected.'>'.$role.'</option>'."\r\n";
		}

		$event_output['role'] .= '</select>';
		unset($abbr); unset($role);



		// ----------- Status
		$event_output['status'] = '
			<select name="user_status" id="user_status">';

		foreach ($mConfig['status_menu'] as $key => $status)
		{
			$event_output['status'] .= '<option value="'.$key.'">
			'.$status.'</option>'."\r\n";
		}

		$event_output['status'] .= '</select>';
		unset($key); unset($status);



		// ----------- Action
		$event_output['action'] = '<input type="submit" value="Subscribe" />';
	}






	echo '
	<!-- Begin Row -->
	<TR class="eventinfo">


	<!-- Subscribe Form -->
	'.$event_output['form'].'


	<!-- Date -->
	<TD>'.$event_output['date'].'</TD>


	<!-- Location -->
	<TD>'.$event_output['location'].'</TD>


	<!-- Invite Time -->
	<TD>'.$event_output['invite_time'].'</TD>


	<!-- Start Time -->
	<TD>'.$event_output['start_time'].'</TD>


	<!-- Attending -->
	<TD>'.$event_output['attending'].'</TD>


	<!-- Characters -->
	<TD>'.$event_output['character'].'</TD>


	<!-- Role -->
	<TD>'.$event_output['role'].'</TD>


	<!-- Status -->
	<TD>'.$event_output['status'].'</TD>


	<!-- Action -->
	<TD>'.$event_output['action'].'</TD>


	<!-- End Row -->
	'.$event_output['form2'].'</TR>';





	/**
	 *	Print characters that are signed up for this event.
	 *
	 *		- each character that the user has subscribed for
	 *		this event is printed in a new row below the main
	 *		event row. It includes a form so the user can update
	 *		the individual characters subscribed for this event.
	 */

	for ($i = 0; $i < count($event->chars_subscribed['character']); $i++)
	{
		// Clear the character output array.
		unset($char_output);
		$char_output = array();



		// ----------- Character
		$char_output['character'] = ucwords($event->chars_subscribed['character'][$i]);



		// ----------- Comments
		$char_output['comments'] = '
			Comments: <input name="comment" maxlength="100" size="30"
			value="'.$event->chars_subscribed['comments'][$i].'" />';



		// ----------- Role
		$char_output['role'] = '<select name="user_role" id="user_role">';

		foreach ($mConfig['roles'] as $abbr => $role)
		{
			($abbr == $event->chars_subscribed['role'][$i]) ?
				$select = ' selected="selected"' :
				$select = '';

			$char_output['role'] .= '<option value="'.$abbr.'"'.$select.'>'.$role.'</option>'."\r\n";
		}

		$char_output['role'] .= '</select>';
		unset($abbr); unset($role);



		// ----------- Status
		$char_output['status'] = '<select name="user_status" id="user_status">';

		if ($event->chars_subscribed['status'][$i] == ATTENDING)
		{
			$char_output['status'] .= '<option value="'.ATTENDING.'" selected="selected">Attending</option>'."\r\n";
		}

		foreach ($mConfig['status_menu'] as $key => $status)
		{
			($key == $event->chars_subscribed['status'][$i]) ?
				$select = ' selected="selected"' :
				$select = '';

			$char_output['status'] .= '<option value="'.$key.'"'.$select.'>'.$status.'</option>'."\r\n";
		}

		$char_output['status'] .= '</select>';
		unset($key); unset($status);



		// ----------- Row Color
		switch ($event->chars_subscribed['status'][$i])
		{
			case ATTENDING: 	$char_output['rowcolor'] = 'attending';		break;
			//case AVAILABLE: 	$char_output['rowcolor'] = 'available';		break;
			//case QUEUE: 		$char_output['rowcolor'] = 'queue';			break;
			//case UNAVAILABLE: 	$char_output['rowcolor'] = 'unavailable';	break;
			default:			$char_output['rowcolor'] = 'queue';			break;
		}





		echo '
		<!-- Begin Row -->
		<TR class="'.$char_output['rowcolor'].'">


		<!-- Edit Form -->
		<form action="index.php" method="post" id="character_update_'.strtolower($char_output['character']).'">
		<input type="hidden" name="process" value="subscribe" />
		<input type="hidden" name="event_id" value="'.$event->eid.'" />
		<input type="hidden" name="is_update" value="1" />
		<input type="hidden" name="user_char" value="'.$char_output['character'].'" />


		<!-- Comments -->
		<TD class="buffer"></TD>
		<TD colspan="4">'.$char_output['comments'].'</TD>


		<!-- Character -->
		<TD><a href="http://www.wowarmory.com/character-sheet.xml?r=Kilrogg&n='.
		$char_output['character'].'">'.$char_output['character'].'</a></TD>


		<!-- Role -->
		<TD>'.$char_output['role'].'</TD>


		<!-- Status -->
		<TD>'.$char_output['status'].'</TD>


		<!-- Action -->
		<TD><input type="submit" value="Update" /></TD>
		
		
		<!-- End Row -->
		</form></TR>';
	}
}


echo '</TABLE>';

?>